#ifndef MISTAKEBOOK_H
#define MISTAKEBOOK_H

#include <QWidget>
#include <QListWidget>
#include <QTextEdit>
#include <QLineEdit>
#include <QComboBox>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGroupBox>
#include <QLabel>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonDocument>
#include <QFileDialog>
#include <QMessageBox>
#include <QScrollArea>
#include <QStackedWidget>

// 错题项数据结构
struct MistakeItem {
    QString title;           // 标题
    QString category;        // 分类
    QString description;     // 描述
    QJsonArray steps;        // 解决步骤
    QString date;           // 创建日期
};

// 错题详情页面
class MistakeDetailPage : public QWidget
{
    Q_OBJECT

public:
    explicit MistakeDetailPage(MistakeItem* item, QWidget* parent = nullptr);
    void saveData();

private slots:
    void addStep();
    void removeStep();
    void moveStepUp();
    void moveStepDown();

private:
    MistakeItem* m_item;
    QLineEdit* m_titleEdit;
    QTextEdit* m_descEdit;
    QVBoxLayout* m_stepsLayout;
    QList<QHBoxLayout*> m_stepLayouts;
    QList<QTextEdit*> m_stepEdits;

    void initUI();
    void loadData();
};

// 错题本主页面
class MistakeBook : public QWidget
{
    Q_OBJECT

public:
    explicit MistakeBook(QWidget* parent = nullptr);
    void loadMistakes();
    void saveMistakes();

private slots:
    void onAddMistake();
    void onDeleteMistake();
    void onMistakeSelected(QListWidgetItem* item);
    void onCategoryChanged(const QString& category);
    void onExportTxt();
    void onImportData();

private:
    QList<MistakeItem> m_mistakes;
    QListWidget* m_mistakeList;
    QComboBox* m_categoryCombo;
    QPushButton* m_addButton;
    QPushButton* m_deleteButton;
    QPushButton* m_exportButton;
    QPushButton* m_importButton;
    QStackedWidget* m_contentStack;
    QLabel* m_emptyLabel;

    void initUI();
    void initCategories();
    void refreshMistakeList();
    void showMistakeDetail(int index);
};

#endif // MISTAKEBOOK_H
